package com.ybfenpan.controller.admin.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Admin;
import com.ybfenpan.model.Member;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.taglib.annotation.Token;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
public class MemberActiveController extends BaseController {
	@Resource
	private MemberService memberService;

	@Token(save = true)
	@RequestMapping("admin/member/memberActiveShow.shtml")
	public String show(HttpServletRequest request) throws Exception {
		Map formMap = getParameterMap(request);
		int id = MapUtil.getIntValue(formMap, "id");
		if (0 < id) {

			// 方法一
			// Map<String, Object> member =
			// memberService.queryForObject("getJoinByKey", id);
			// request.setAttribute("member", member);

			// 方法二
			formMap.put("id", id); // 未报单
			List list = memberService.queryForList("memberForList", formMap);

			if (list.size() > 0) {

				// member=list.get(0);
				request.setAttribute("member", list.get(0));
			}

		}
		return "admin/member/memberActive";

	}

	@RequestMapping("admin/member/memberActive.shtml")
	@ResponseBody
	@Transactional
	@Token(remove = true)
	public void update(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception {

		Map formMap = getParameterMap(request);
		int id = MapUtil.getIntValue(formMap, "id");
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String activeLoginName = MapUtil.getString(formMap, "activeLoginName");

		boolean result = true; // 验证结果
		String msg = "";
		if (0 < id) {
			Admin admin = ContextUtil.getAdmin();
			String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword, CryptUtil.keyStr);

			if (!admin.getTwoPassword().equals(pwd)) {
				msg = "管理员安全密码不正确。";
				result = false;

			} else {

				Member member = memberService.getByKey(id);

				// 推荐人验证
				if (member.getRecommendId() != null) {

					Member recommend = memberService.getByKey(member.getRecommendId());
					if (recommend != null) {
						if (recommend.getAccountActive().equals("F")) {
							msg = "推荐人未报单。";
							result = false;
						}
					}

				}

				if (!member.getLoginName().equals(activeLoginName)) {
					msg = "报单账户不正确。";
					result = false;
				}

				if (member.getAccountActive().equals("T")) {
					msg = "该用户已经报单。";
					result = false;
				}

				if (member.getFatherId() != null) {
					Member father = memberService.getByKey(member.getRecommendId());
					if (father != null) {
						if (father.getAccountActive().equals("F")) {

							msg = "该接点人未报单，请先报单推荐人。";
							result = false;
						}
					}
				}
			}

		} else {
			msg = "请选择报单用户。";
			result = false;
		}

		if (result) {
			 memberService.saveActive(id,null,0);
		}

		HashMap resultformMap = new HashMap();
		resultformMap.put("msg", msg);
		renderSuccess(response, result, resultformMap);

	}

}
